import { DEFAULTLAYER } from '@/libs/constant'

class Circle {
  constructor(props) {
    this.type = 'circle'
    this.layer = DEFAULTLAYER
    this.startX = props?.startX || 0
    this.startY = props?.startY || 0
    this.endX = this.startX
    this.endY = this.startY
  }

  get minX() {
    return Math.min(this.startX, this.endX)
  }

  get minY() {
    return Math.min(this.startY, this.endY)
  }

  get maxX() {
    return Math.max(this.startX, this.endX)
  }

  get maxY() {
    return Math.max(this.startY, this.endY)
  }

  get radiusX() {
    return (this.endX - this.startX) / 2
  }

  get radiusY() {
    return (this.endY - this.startY) / 2
  }

  get centerX() {
    return this.startX + this.radiusX
  }

  get centerY() {
    return this.startY + this.radiusY
  }

  getType() {
    return this.type
  }

  getLayer() {
    return this.layer
  }

  setEnd(x, y) {
    this.endX = x
    this.endY = y
  }

  changePosition(diffX, diffY) {
    this.startX += diffX
    this.startY += diffY
    this.endX += diffX
    this.endY += diffY
  }

  inside(x, y) {
    if (x > this.minX && x < this.maxX && y > this.minY && y < this.maxY) {
      return true
    }
    return false
  }

  draw(ctx) {
    const centerX = Math.abs(this.centerX)
    const centerY = Math.abs(this.centerY)
    const radiusX = Math.abs(this.radiusX)
    const radiusY = Math.abs(this.radiusY)
    ctx.beginPath()
    ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI)
    ctx.stroke()
  }
}

export default Circle
